package com.imooc.distributedemo.resume;

import java.util.concurrent.atomic.AtomicReference;

public class SingletonCAS {
    private static final AtomicReference<SingletonCAS> INSTANCE = new AtomicReference<SingletonCAS>();

    private SingletonCAS(){

    }

    public static SingletonCAS getInstance(){
        for(;;){
            SingletonCAS singletonCAS = INSTANCE.get();

            if(null != singletonCAS){
                return singletonCAS;
            }
            singletonCAS = new SingletonCAS();
            if(INSTANCE.compareAndSet(null,singletonCAS)){
                return singletonCAS;
            }
        }
    }
}
